package com.umeox.ota.engine;


import com.umeox.ota.utils.ActivityCollector;
import com.abupdate.fotainject.FotaInjector;
import com.abupdate.iot_libs.data.constant.OtaConstants;
import com.abupdate.iot_libs.data.remote.PolicyMapInfo;
import com.abupdate.iot_libs.data.remote.VersionInfo;
import com.abupdate.iot_libs.interact.callback.IParsePolicyListener;
import com.abupdate.sdk_annotation.Inject;

/**
 * @author fighter_lee
 * @date 2018/2/4
 * 自定义强制下载策略
 */
public class PolicyForceDownload implements IParsePolicyListener {

    @Inject
    VersionInfo versionInfo;

    @Override
    public boolean doParse() {
        FotaInjector.inject(this);
        //获取后台配置策略
        if (getServerConfig()) {
            if (appIsShowing()) {
                return false;
            }
            return true;
        }
        return false;
    }

    private boolean getServerConfig() {
        PolicyMapInfo info = null;
        try {
            info = versionInfo.policyHashMap.get(OtaConstants.KEY_DOWNLOAD_FORCE);
        } catch (Exception e) {
            return false;
        }
        if (null != info) {
            if ("true".equals(info.key_value)) {
                return true;
            }
        }
        return false;
    }

    private boolean appIsShowing() {
        return ActivityCollector.isShowing();
    }
}
